//
//  AESHelp.m
//  AllMedia
//
//  Created by admin on 13-7-17.
//
//

#import "AESHelp.h"
#import "AESCrypt.h"
#import "NSData+Encryption.h"
#import "NSString+Base64.h"
#import "NSData+Base64.h"
#import "NSString+Clear.h"
@implementation AESHelp

+(NSString *)encrypt:(NSString *)message
{
    NSString *string = [NSString stringWithString:message];
    if (NEEDED_ENCRYPT) {
        return [AESCrypt encrypt:string passwordData:[self keyData]];
    }
    return string;
}

+(NSString *)decrypt:(NSString *)base64EncodedString
{
    NSString *string = nil;
    if (NEEDED_ENCRYPT) {
        NSString *decryptStr = [AESCrypt decrypt:base64EncodedString passwordData:[self keyData]];
        string = [[NSString alloc] initWithFormat:@"%@",decryptStr];
    }
    else {
        string = [[NSString alloc] initWithFormat:@"%@",base64EncodedString];
    }
    return string;
}

#pragma mark - 2 used

//<<< 2 >>>

+(NSData *)encrypt2Data:(NSData *)data
{
    if (data == nil) {
        return data;
    }
    
    return [NSData dataWithData:[data AES256EncryptWithKey:[self keyData]]];
}

+(NSData *)decrypt2Data:(NSData *)data
{
    if (data == nil) {
        return data;
    }
    
    return [NSData dataWithData:[data AES256DecryptWithKey:[self keyData]]];
}

+(NSString *)encryptMothed2:(NSString *)message
{
    if (message == nil || [message stringWithNothing:message]) {
        return message;
    }
    
    NSData *plainTextData = [message dataUsingEncoding:NSUTF8StringEncoding];
    NSData *cipherTextData = [plainTextData AES256EncryptWithKey:[self keyData]];
    NSString *base64String = [NSString base64StringFromData:cipherTextData length:[cipherTextData length]];
    
    return base64String;
}

+(NSString *)decryptMothed2:(NSString *)base64EncodedString
{
    if (base64EncodedString == nil || [base64EncodedString stringWithNothing:base64EncodedString]) {
        return base64EncodedString;
    }
    
    NSData *encryptedData = [NSData base64DataFromString:base64EncodedString];
    NSData *decryptedData = [encryptedData AES256DecryptWithKey:[self keyData]];
    return [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];
}

//<<<<<<<<<<<<图片>>>>>>>>>>>

+(NSData *)encryptMothed2WithImageData:(NSData*)imageData
{
    return [imageData AES256EncryptWithKey:[self keyData]];
}

+(NSData *)decryptMothed2WithImageData:(NSData*)imageData
{
    return [NSData dataWithData:[imageData AES256DecryptWithKey:[self keyData]]];
}

#pragma mark -

+(NSData *)keyData
{
    static Byte keyByte[] = {0x08,0x08,0x04,0x0b,0x02,0x0f,0x0b,0x0c,0x01,0x03,0x09,0x07,0x0c,0x03,
        0x07,0x0a,0x04,0x0f,0x06,0x0f,0x0e,0x09,0x05,0x01,0x0a,0x0a,0x01,0x09,
        0x06,0x07,0x09,0x0d};
    //byte转换为NSData类型，以便下边加密方法的调用
    NSData *keyData = [[NSData alloc] initWithBytes:keyByte length:32];
    return keyData ;
}
@end
